package com.rock.code.common.util;

import java.io.File;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author Marka
 * @date 2015/12/13
 * @Time 13:59
 * @Version 1.0
 */
public class RemoteFilesAccessUtil {

    /**
     * get remote file list
     */
    public void listFiles() {
        Properties pps = new Properties();
        InputStream in = null;
        try {
            Properties prop = new Properties();
            in = Object.class.getResourceAsStream("/config.properties");
            prop.load(in);
            String path = prop.getProperty("remote_file_path");
            //String path = "\\\\10.0.113.158\\log";
            File file = new File(path);
            File[] files = file.listFiles();
            System.out.println("path:" + path + " file:" + file + " files:" + files);
            for (File f : files) {
                if (f.isDirectory()) {
                    System.out.println(" dir : " + f.getAbsolutePath());
                } else {
                    System.out.println("file : " + f.getAbsolutePath());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getStrutsConfig() {
        String strutsPath = new String("struts-default.xml,struts-plugin.xml,struts.xml");
        String path = getClass().getClassLoader().getResource("").getPath();
        path = path.substring(0, path.lastIndexOf("/"));
        path = path.substring(0, path.lastIndexOf("/") + 1);
        //System.out.println("==============================>>>" + path);
        File file = new File(path);
        StringBuilder sb = new StringBuilder();
        scanDirRecursion(file, sb);
        System.out.println("==============================>>>" + sb);
        strutsPath += sb.toString();
        //System.out.println("---------------------->>>" + strutsPath);
    }

    public void scanDirRecursion(File file, StringBuilder sb) {
        try {
            System.out.println("-=-=--==-=-=" + file.isFile());
            if (file.isDirectory()) {
                String[] files = file.list();
                if (files != null) {
                    for (int i = 0; i < files.length; i++) {
                        scanDirRecursion(new File(file, files[i]), sb);
                    }
                }
            } else {
                System.out.println(file.getAbsolutePath() + "===" + file.getCanonicalPath() + "<<<--->>>" + file.getName());
                if (file.getName().endsWith(".xml")) {
                    sb.append(",").append(file.getAbsolutePath());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //new RemoteFilesAccessUtil().listFiles();
        new RemoteFilesAccessUtil().getStrutsConfig();
    }
}
